import audioManager from '../../general/audioManager'
import { util } from '../../general/util'
import GamePanel from '../../hall/gamePanel'
const { ccclass, property } = cc._decorator
@ccclass
export class game728 extends cc.Component {
  @property(cc.Label)
  score_label: cc.Label = null
  @property(cc.Label)
  max_label: cc.Label = null
  @property(cc.Node)
  frame_node: cc.Node = null
  @property(cc.Node)
  role_Node: cc.Node = null
  @property(cc.Node)
  seat_pre: cc.Node = null
  @property(cc.Node)
  content_node: cc.Node = null
  @property(cc.SpriteFrame)
  spListA: cc.SpriteFrame[] = []
  @property(cc.SpriteFrame)
  spListB: cc.SpriteFrame[] = []
  @property(cc.AudioClip)
  mp3: cc.AudioClip[] = []
  isFire: number = 0
  isGameOver: boolean = false
  costDis = 0.5
  isTouchEnd: boolean = false
  lastIndex = 0
  lenList = [480, 430, 330, 160, 100]
  isHit: boolean = false
  currentAction = null
  onLoad() {
    this.initData()
  }
  /**初始化数据 */
  initData() {
    this.max_label.string = '0'
    this.score_label.string = '0'
    this.node.on(cc.Node.EventType.TOUCH_START, this.tochStart, this)
    this.node.on('checkGameOver', this.checkGameOver, this)
    this.node.on('onhit', this.onhit, this)
    this.creatOneSeat()
  }
  updateScore(score) {
    GamePanel.instance.updateScore(score)
  }
  checkGameOver() {
    if (this.isGameOver) {
      return
    }
    this.isGameOver = true
    console.log('结束')
    this.scheduleOnce(() => {
      GamePanel.instance.showGameStatis()
    }, 1)
  }
  onhit(self: cc.Node, other: cc.Node) {
    this.isHit = true
    self.getChildByName('sp').getComponent(cc.Sprite).spriteFrame = this.spListA[self['index']]
    other.getChildByName('lizi').getComponent(cc.ParticleSystem).resetSystem()
    self.stopAllActions()
  }
  /**触摸发射 */
  tochStart(event: cc.Event.EventTouch) {
    if (this.isTouchEnd) {
      return
    }
    if (this.isTouchEnd) {
      return
    }
    audioManager.playEffect(this.mp3[0])
    this.content_node.runAction(cc.moveBy(0.5, 0, -200))
    this.role_Node.getChildByName('sp').getComponent(cc.Animation).play()
    this.role_Node.runAction(
      cc.sequence(
        cc.jumpBy(0.5, cc.v2(0, 0), 250, 1),
        cc.callFunc(() => {
          cc.director.getCollisionManager().enabled = true
          this.scheduleOnce(() => {
            cc.director.getCollisionManager().enabled = false
            if (this.isHit) {
              this.updateScore(10)
              this.isHit = false
              this.creatOneSeat()
            } else {
              this.checkGameOver()
              this.content_node.children.forEach((v) => {
                v.stopAllActions()
                v.runAction(cc.spawn(cc.moveBy(1, 0, -700), cc.rotateBy(0.3, 60)))
              })
            }
          })
        })
      )
    )
  }
  creatOneSeat() {
    const pre = cc.instantiate(this.seat_pre)
    this.content_node.addChild(pre)
    pre.active = true
    pre.y = -55 + this.lastIndex * 200
    const index = util.getRandomInt(0, this.spListA.length - 1)
    pre.getComponent(cc.BoxCollider).size.width = this.lenList[index]
    pre.getChildByName('sp').getComponent(cc.Sprite).spriteFrame = this.spListB[index]
    this.lastIndex++
    pre['index'] = index
    pre.getComponent('seatpre728')['speed'] = this.lastIndex * 0.05 < 2.8 ? 3 - this.lastIndex * 0.05 : 0.2
  }
}
